package cn.edu.hust.sse.software7.class3.DAO;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBShoppingCar;

public class EBShoppingCarDAO {
	private static final Logger log = Logger.getLogger(EBShoppingCarDAO.class
			.getName());

	public void createSCar(EBShoppingCar shoppingCar) {
		log.info("add shopping car for user " + shoppingCar.getUserId()
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(shoppingCar);
			tx.commit();
			log.finest("add shopping car for user " + shoppingCar.getUserId()
					+ " success.");
		} catch (Exception e) {
			log.info("add shopping car for user " + shoppingCar.getUserId()
					+ "failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}	

	public List<EBShoppingCar> getByuserid(Long userId) {

		log.info("get shopping car by userId " + userId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBShoppingCar> cars = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBShoppingCar.class);
			query.setFilter("userId == " + userId + "");
			cars =(List<EBShoppingCar>) query.execute();
			tx.commit();
			log.finest("get shopping car by userId " + userId + " success.");
			
		} catch (Exception e) {
			log.info("get shopping car by userId " + userId + " failed!!!\n"
					+ e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return cars;
	}

	public void clearSCar(Long userId) {
		log.info("clearSCar user " + userId	+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Query query = pm.newQuery(EBShoppingCar.class);
			query.setFilter("userId == " +userId);
			
			List<EBShoppingCar> cars = (List<EBShoppingCar>) query.execute();
			pm.deletePersistentAll(cars);
			tx.commit();
			log.finest("clearSCar user " + userId	+ " success.");
		} catch (Exception e) {
			log.info("clearSCar user " + userId	+ " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteCarById(EBShoppingCar car) {
		log.info("delete shopping car begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Query query = pm.newQuery(EBShoppingCar.class);
			query.setFilter("userId == " +car.getUserId()+	" && productId=="+ car.getProductId());
			EBShoppingCar car2D = ((List<EBShoppingCar>)query.execute()).get(0);
			pm.deletePersistent(pm.getObjectById(EBShoppingCar.class, car2D.getShopcarId()));
			tx.commit();
			log.finest("delete shopping car success.");
		} catch (Exception e) {
			log.info("delete shopping car failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public boolean isExistCar(EBShoppingCar car) {
		log.info("check car exist begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		boolean b = false ;
		try {
			tx.begin();
			Query query = pm.newQuery(EBShoppingCar.class);
			query.setFilter("userId == "+car.getUserId() +" && productId == "+car.getProductId());
			List<EBShoppingCar> cars = (List<EBShoppingCar>) query.execute();
			if(null != cars && !cars.isEmpty()){
				b = true;
			}
			tx.commit();
			log.finest("check car exist  success.");
		} catch (Exception e) {
			log.info("check car exist failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return b;
	}
}
